<template>
  <div class="news-card" @click="goToDetail">
    <div class="news-image">
      <div class="image-placeholder">
        <img :src="`/api/files/${news.coverImage}`" :alt="news.title" v-if="news.coverImage" />
        <div v-else class="default-image">
          <picture-outlined />
        </div>
      </div>
      <div class="news-category">{{ getCategoryName(news.category) }}</div>
    </div>
    <div class="news-content">
      <h3 class="news-title">{{ news.title }}</h3>
      <p class="news-summary">{{ news.summary }}</p>
      <div class="news-meta">
        <div class="meta-left">
          <span class="news-author">
            <user-outlined />
            {{ news.author }}
          </span>
          <span class="news-date">
            <calendar-outlined />
            {{ formatDate(news.publishTime) }}
          </span>
        </div>
        <div class="meta-right">
          <span class="news-views">
            <eye-outlined />
            {{ formatViewCount(news.viewCount) }}
          </span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router'
import { 
  PictureOutlined, 
  UserOutlined, 
  CalendarOutlined, 
  EyeOutlined 
} from '@ant-design/icons-vue'
import dayjs from 'dayjs'

const router = useRouter()

const props = defineProps({
  news: {
    type: Object,
    required: true
  }
})

// 格式化日期
const formatDate = (date) => {
  return dayjs(date).format('MM-DD')
}

// 格式化浏览量
const formatViewCount = (count) => {
  if (count >= 10000) {
    return (count / 10000).toFixed(1) + 'w'
  } else if (count >= 1000) {
    return (count / 1000).toFixed(1) + 'k'
  }
  return count.toString()
}

// 获取分类名称
const getCategoryName = (category) => {
  const categoryMap = {
    'important': '学校要闻',
    'academic': '学术动态',
    'general': '综合新闻',
    'notice': '通知公告'
  }
  return categoryMap[category] || '新闻'
}

// 跳转到详情页
const goToDetail = () => {
  router.push(`/news/${props.news.id}`)
}
</script>

<style scoped>
.news-card {
  display: flex;
  flex-direction: column;
  background-color: #fff;
  border-radius: 12px;
  overflow: hidden;
  transition: all 0.3s ease;
  cursor: pointer;
  height: 100%;
  border: 1px solid #f0f0f0;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

.news-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
  border-color: #d1d5db;
}

.news-image {
  height: 200px;
  overflow: hidden;
  position: relative;
}

.image-placeholder {
  width: 100%;
  height: 100%;
  position: relative;
}

.news-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.default-image {
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #f3f4f6 0%, #e5e7eb 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #9ca3af;
  font-size: 32px;
}

.news-card:hover .news-image img {
  transform: scale(1.05);
}

.news-category {
  position: absolute;
  top: 12px;
  left: 12px;
  background: rgba(59, 130, 246, 0.9);
  color: #fff;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
  backdrop-filter: blur(4px);
}

.news-content {
  padding: 20px;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.news-title {
  font-size: 16px;
  font-weight: 600;
  margin: 0 0 12px;
  color: #1f2937;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.4;
  transition: color 0.3s ease;
}

.news-card:hover .news-title {
  color: #3b82f6;
}

.news-summary {
  font-size: 14px;
  color: #6b7280;
  margin-bottom: 16px;
  flex: 1;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.5;
}

.news-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  color: #9ca3af;
  border-top: 1px solid #f3f4f6;
  padding-top: 12px;
}

.meta-left {
  display: flex;
  gap: 12px;
}

.meta-right {
  display: flex;
  align-items: center;
}

.news-author,
.news-date,
.news-views {
  display: flex;
  align-items: center;
  gap: 4px;
  font-weight: 500;
}

.news-views {
  color: #3b82f6;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .news-image {
    height: 160px;
  }
  
  .news-content {
    padding: 16px;
  }
  
  .news-title {
    font-size: 15px;
  }
  
  .news-summary {
    font-size: 13px;
  }
  
  .meta-left {
    flex-direction: column;
    gap: 4px;
  }
}

.news-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  color: #999;
}

.news-author {
  max-width: 30%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.news-date, .news-views {
  display: flex;
  align-items: center;
  gap: 4px;
}

@media (max-width: 768px) {
  .news-image {
    height: 150px;
  }
  
  .news-title {
    font-size: 15px;
  }
  
  .news-summary {
    font-size: 13px;
    -webkit-line-clamp: 2;
  }
}
</style>